﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
//using System.Windows.Interop;

namespace LeapInterface
{

    [Serializable, StructLayout(LayoutKind.Sequential)]
    struct SCROLLINFO
    {
        public uint cbSize;
        public uint fMask;
        public int nMin;
        public int nMax;
        public uint nPage;
        public int nPos;
        public int nTrackPos;
    }

    public enum ScrollInfoMask : uint
    {
        SIF_RANGE = 0x1,
        SIF_PAGE = 0x2,
        SIF_POS = 0x4,
        SIF_DISABLENOSCROLL = 0x8,
        SIF_TRACKPOS = 0x10,
        SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
    }

    class MouseCursor
    {


        [DllImport("user32.dll")]
        private static extern bool SetCursorPos(int x, int y);

        [DllImport("user32.dll")]
        static extern void mouse_event(int dwFlags, int dx, int dy,
                      int dwData, int dwExtraInfo);

        [DllImport("user32.dll")]
        static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

        [DllImport("user32.dll")]
        public static extern int GetScrollPos(IntPtr hwnd, int nBar);

        [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
        static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

        [Flags]
        public enum MouseEventFlags
        {
            LEFTDOWN = 0x00000002,
            LEFTUP = 0x00000004,
            MIDDLEDOWN = 0x00000020,
            MIDDLEUP = 0x00000040,
            MOVE = 0x00000001,
            ABSOLUTE = 0x00008000,
            RIGHTDOWN = 0x00000008,
            RIGHTUP = 0x00000010,
            HWHEEL = 0x1000,
            WHEEL = 0x0800
        }


        public static void MoveCursor(int x, int y)
        {
            SetCursorPos(x, y);
        }

        public static void LeftClick()
        {
            mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
            mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
        }

        public static void LeftUp()
        {
            mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
        }

        public static void LeftDown()
        {
            mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
        }

        public static void RightClick()
        {
            mouse_event((int)(MouseEventFlags.RIGHTDOWN), 0, 0, 0, 0);
            mouse_event((int)(MouseEventFlags.RIGHTUP), 0, 0, 0, 0);
        }



        public static void ScrollTo(int Position)
        {
            SetScrollPos((IntPtr)GetForegroundWindow(), 0x1, Position, true); //work only with the focused windows
            PostMessage((IntPtr)GetForegroundWindow(), 0x115, 4 + 0x10000 * Position, 0);
        }

        public static void ScrollLeft(int wheelMov)
        {
            mouse_event((int)(MouseEventFlags.HWHEEL), 0, 0, -wheelMov, 0);
        }

        public static void ScrollRight(int wheelMov)
        {
            mouse_event((int)(MouseEventFlags.HWHEEL), 0, 0, wheelMov, 0);
        }

        public static void ScrollDown(int wheelMov)
        {
            mouse_event((int)(MouseEventFlags.WHEEL), 0, 0, -wheelMov, 0);
        }

        public static void ScrollUp(int wheelMov)
        {
            mouse_event((int)(MouseEventFlags.WHEEL), 0, 0, wheelMov, 0);
        }
    }
}
